package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.CommonDao;
import cn.edu.sdjzu.xg.bysj.dao.DegreeDao;
import cn.edu.sdjzu.xg.bysj.domain.Degree;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;


public final class DegreeService {
    private static DegreeDao degreeDao = DegreeDao.getInstance();
    private static DegreeService degreeService = new DegreeService();


    public static DegreeService getInstance() {
        return degreeService;
    }

    public Collection<Degree> findAll(List<Condition> conditionList_json_str,
                                      Pagination pagination)
            throws SQLException {

        Connection connection = JdbcHelper.getConn();
        Collection<Degree> degrees = degreeDao.findAll(connection, conditionList_json_str, pagination);
        JdbcHelper.close(connection);
        return degrees;
    }


    public Degree find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Degree result = degreeDao.find(id, connection);
        JdbcHelper.close(connection);
        return result;
    }

    public boolean update(Degree degree) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            updated = degreeDao.update(degree, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为true
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(Degree degree) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean added = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            added = degreeDao.add(degree, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为true
            connection.setAutoCommit(true);
            //关闭连接
            JdbcHelper.close(connection);
        }
        return added;
    }

    public boolean delete(Integer id) throws SQLException, BysjException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        Degree degree = this.find(id);
        boolean deleted = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            deleted = degreeDao.delete(degree, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为true
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public boolean delete(Degree degree_json, Connection connection) throws SQLException, BysjException {
        boolean result = false;
        int countTeacherOfProfTitle = CommonDao.getInstance().
                countWhereForeignKey(connection, "teacher", "degree_id", degree_json.getId());
        //若没有二级单位，则能够删除
        if (countTeacherOfProfTitle == 0) {
            result = degreeDao.delete(degree_json, connection);
        } else {
            throw new BysjException(degree_json.getDescription() + "有下属系，不能删除");
        }
        JdbcHelper.close(connection);
        return result;
    }

    public static class BysjException extends Exception {
        public BysjException(String message) {
            super(message);
        }
    }
}


